﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CommDll;

namespace HospitalMedicineTraceability
{
    /// <summary>
    /// ClassName:frmDbConfig
    /// Version:1.0
    /// Date:2022-06-23
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为实现数据库连接设置
    /// </remarks>
    public partial class frmDbConfig_WS : DevComponents.DotNetBar.OfficeForm
    {
        #region 0.自定义属性
        #endregion

        public frmDbConfig_WS()
        {
            InitializeComponent();
        }

        #region 1.自定义方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void _init()
        {
            // 01 读数据库连接信息
            txtServerName.Text = AccessDB_WS.AccessDB.m_strWebserviceUrl;
            txtDbName.Text = AccessDB_WS.AccessDB.m_strDbName;
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        private void _saveInfo()
        {
            string strTemp = "";
            // 01 保存数据库连接信息
            AccessDB_WS.AccessDB.m_strWebserviceUrl = txtServerName.Text.Trim();
            AccessDB_WS.AccessDB.m_strDbName = txtDbName.Text.Trim();
            strTemp = "{0};{1}";
            strTemp = string.Format(strTemp, AccessDB_WS.AccessDB.m_strWebserviceUrl, AccessDB_WS.AccessDB.m_strDbName);
            // 02 数据库连接成功则保存到AppConfig
            if (AccessDB_WS.AccessDB._connectDB(AccessDB_WS.AccessDB.m_strWebserviceUrl, AccessDB_WS.AccessDB.m_strDbName))
            {
                // 更新数据库连接字符串信息
                strTemp = FunctionComm._encryptDES(strTemp, "taihao12");
                FunctionComm._setAppConfigKeyValue("ConnectionString", strTemp);
                CommFun._messageBoxShow("数据库连接成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                CommFun._messageBoxShow("数据库连接失败,请重新设置！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion

        #region 2.系统事件方法
        private void frmDbConfig_Load(object sender, EventArgs e)
        {
            _init();            
        }

        private void frmDbConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!AccessDB_WS.AccessDB.m_bIsConnected)
            {
                //数据库联接失败提示
                DialogResult result = CommFun._messageBoxShow("是否确定退出数据库设置，系统将不能继续运行?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _saveInfo();
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}